

#import "smPhotoItemView.h"
//#import "NSString+SBJSON.h"
#import "ViewUtil.h"

#define ___IMG_URL__ @""

@interface smPhotoItemView() <UpLoadImageDelegate>

@property (nonatomic) bool isChanged;
@property (nonatomic) bool isDefaultImg;

@property (nonatomic, strong) HTTPImageView* myShopPhotoHttpImageView;
@property (nonatomic, strong) NSString* imageUrlShortName;
@property (nonatomic, strong) UIButton* changedImgButton;
@property (nonatomic, strong) UIButton* deleteImgButton;

-(void) addPhotobuttonClicked:(UIButton*) button;

@end


@implementation smPhotoItemView

@synthesize isChanged;
@synthesize isDefaultImg;
@synthesize isUpLoadSuccess;
@synthesize isUpLoadFailed;

@synthesize myShopPhotoHttpImageView = _myShopPhotoHttpImageView;
@synthesize imageUrlShortName = _imageUrlShortName;
@synthesize deleteImgButton = _deleteImgButton;
@synthesize changedImgButton = _changedImgButton;
@synthesize uploadImageDateSource = _uploadImageDateSource;

@synthesize uploadimgDelegate;
@synthesize addShopimgDelegate;


- (id)initWithFrame:(CGRect)frame {
    
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }

    self.myShopPhotoHttpImageView = [[HTTPImageView alloc] initWithFrame:self.bounds];
    self.isChanged = NO;
    self.isDefaultImg = YES;
    self.isUpLoadSuccess = NO;
    self.isUpLoadFailed = NO;

    [self addSubview:self.myShopPhotoHttpImageView];

    UIView* tempViewNor = [[UIView alloc] initWithFrame:self.bounds];
    [tempViewNor setBackgroundColor:[UIColor colorWithRed:238.0/255 green:238.0/255 blue:238.0/255 alpha:1.0]];
    UIImageView* tempImageNorViewBack = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"addBackNor.png"]];
    UIImageView* tempImageNorViewiCon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"addPic.png"]];
    [tempImageNorViewiCon setFrame:CGRectMake(230, 200, 36, 36)];
    [tempViewNor addSubview:tempImageNorViewBack];
    [tempViewNor addSubview:tempImageNorViewiCon];
    UILabel* textNorLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 240, 512, 40)];
    [textNorLabel setBackgroundColor:[UIColor clearColor]];
    [textNorLabel setFont:[UIFont systemFontOfSize:30.0]];
    [textNorLabel setContentMode:UIViewContentModeCenter];
    [textNorLabel setTextAlignment:UITextAlignmentCenter];
    [textNorLabel setText:@"拍个帅照吧"];
    [textNorLabel setTextColor:[UIColor colorWithRed:102.0/255 green:102.0/255 blue:102.0/255 alpha:1.0]];
    [tempViewNor addSubview:textNorLabel];
    
    UIView* tempViewFoc = [[UIView alloc] initWithFrame:self.bounds];
    [tempViewFoc setBackgroundColor:[UIColor colorWithRed:238.0/255 green:238.0/255 blue:238.0/255 alpha:1.0]];
    UIImageView* tempImageFocViewBack = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"addBackFoc.png"]];
    UIImageView* tempImageFocViewiCon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"addPic.png"]];
    [tempImageFocViewiCon setFrame:CGRectMake(230, 200, 36, 36)];
    [tempViewFoc addSubview:tempImageFocViewBack];
    [tempViewFoc addSubview:tempImageFocViewiCon];
    UILabel* textFocLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 240, 512, 40)];
    [textFocLabel setBackgroundColor:[UIColor clearColor]];
    [textFocLabel setFont:[UIFont systemFontOfSize:30.0]];
    [textFocLabel setContentMode:UIViewContentModeCenter];
    [textFocLabel setTextAlignment:UITextAlignmentCenter];
    [textFocLabel setText:@"拍个帅照吧"];
    [textFocLabel setTextColor:[UIColor colorWithRed:102.0/255 green:102.0/255 blue:102.0/255 alpha:1.0]];
    [tempViewFoc addSubview:textFocLabel];
   
    self.changedImgButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.changedImgButton setImage:[ViewUtil imageWithView:tempViewNor] forState:UIControlStateNormal];
    [self.changedImgButton setImage:[ViewUtil imageWithView:tempViewFoc] forState:UIControlStateHighlighted];
    [self.changedImgButton setFrame:self.bounds];
    [self.changedImgButton addTarget:self action:@selector(addPhotobuttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.changedImgButton setBackgroundColor:[UIColor clearColor]];
    [self addSubview:self.changedImgButton];
        
    return self;
}


-(void) addDeleteButton {

    if (self.deleteImgButton == nil) {
        self.deleteImgButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.deleteImgButton setFrame:CGRectMake(self.bounds.origin.x+450, self.bounds.origin.y+5, 50, 50)];
        [self.deleteImgButton setImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
        [self.deleteImgButton addTarget:self action:@selector(deleteShopPhotoImage:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.deleteImgButton];        
    }
    else {
        [self.deleteImgButton setHidden:NO];
    }
}


-(void) deleteShopPhotoImage:(UIButton*) button {
    
    [self setShopImage:[UIImage imageNamed:@"no_picture_80x80.png"]];
    self.isDefaultImg = YES;
    
    if (self.deleteImgButton != nil) {
        [self.deleteImgButton setHidden:YES];
    }
    
    self.imageUrlShortName = nil;
    [self.changedImgButton setHidden:NO];
}


-(void) setShopDefaultImage:(UIImage*) image {
    
    [self.myShopPhotoHttpImageView setImage:nil];
    [self.myShopPhotoHttpImageView setPlaceHolder:image];
    [self.deleteImgButton setHidden:YES];
    self.isDefaultImg = YES;
    self.isUpLoadSuccess = NO;
}


-(void) setShopImage:(UIImage*) image {
    
    [self.myShopPhotoHttpImageView setImage:image];
    [self addDeleteButton];
}


-(void) setShopNewImage:(UIImage*) image {
    
    self.isChanged = YES;
    self.isDefaultImg = NO;
    self.isUpLoadSuccess = NO;
    [self setShopImage:image];
    
    //Hidden ImgAddButton
    [self.changedImgButton setHidden:YES];
}


-(void) upLoadImageToSever {
    
    if (self.isDefaultImg == NO && self.isChanged == YES && self.isUpLoadSuccess == NO) {
        
        self.uploadImageDateSource = [[UpLoadImageModel alloc] init];
        [self.uploadImageDateSource setDelegate:self];
        
        self.isUpLoadFailed = NO;
        
        [self.uploadImageDateSource upLoadImages:self.myShopPhotoHttpImageView.image];
       
    }
}


-(void) setmyShopHttpImgUrl:(NSString*) shortUrl {
    
    if (shortUrl == nil) {
        return;
    }
    
    self.imageUrlShortName = shortUrl;
    [self.myShopPhotoHttpImageView setUrl:self.imageUrlShortName];
     //[NSString stringWithFormat:@"%@%@_120x120.jpg",___IMG_URL__,self.imageUrlShortName]];
    
    self.isDefaultImg = NO;
    [self.changedImgButton setHidden:YES];
    
    [self addDeleteButton];
}


-(void) addPhotobuttonClicked:(UIButton*) button {
    
    if (self.isDefaultImg == NO) {
        return;
    }
    
    if (self.addShopimgDelegate && [self.addShopimgDelegate respondsToSelector:@selector(addShopPhoto:)]) {
        [self.addShopimgDelegate performSelector:@selector(addShopPhoto:)withObject:self];
    }
}


- (void) postDataFinished:(id)sel {
    NSString* tempStr = self.uploadImageDateSource.SuccessReturnStr;
    
    NSDictionary *jsonValue = @"";//[tempStr JSONValue]; 

    //ERR_SID_INVALID::SESSION过期
    
    NSArray* retArray = [jsonValue objectForKey:@"ret"];
    
    if ([retArray count] > 0) {
        if ([([[jsonValue objectForKey:@"ret"] objectAtIndex:0]) isEqualToString:@"ERR_SID_INVALID::SESSION过期"]) {
            self.isUpLoadSuccess = NO;
            self.isChanged = YES;
            if (uploadimgDelegate && [uploadimgDelegate respondsToSelector:@selector(postDataFinished:)]) {
                [uploadimgDelegate performSelector:@selector(postDataFailed:)withObject:self];
            }
            return;
        }
        else if ([([[jsonValue objectForKey:@"ret"] objectAtIndex:0]) isEqualToString:@"ERRCODE_AUTH_REJECT::非法的请求"]){
            self.isUpLoadSuccess = NO;
            self.isChanged = YES;
            if (uploadimgDelegate && [uploadimgDelegate respondsToSelector:@selector(postDataFinished:)]) {
                [uploadimgDelegate performSelector:@selector(postDataFailed:)withObject:self];
            }
            return;
        }
    }
 
    self.imageUrlShortName = [[jsonValue objectForKey:@"data"]objectForKey:@"ftsfile"];
    
    NSLog(self.imageUrlShortName, nil);

    self.isUpLoadSuccess = YES;
    self.isChanged = NO;
    if (uploadimgDelegate && [uploadimgDelegate respondsToSelector:@selector(postDataFinished:)]) {
        [uploadimgDelegate performSelector:@selector(postDataFinished:)withObject:self];
    }
}


- (void) postDataFailed:(id)sel {
    //NSString* tempStr = self.uploadImageDateSource.FailReturnStr;
    
    self.isUpLoadFailed = YES;
    
    if (uploadimgDelegate && [uploadimgDelegate respondsToSelector:@selector(postDataFailed:)]) {
        [uploadimgDelegate performSelector:@selector(postDataFailed:)withObject:self];
    }
}


- (void) postDataProgress:(id)sel {
    //NSNumber* tempStr = self.uploadImageDateSource.progress;
    
    if (uploadimgDelegate && [uploadimgDelegate respondsToSelector:@selector(postDataFinished:)]) {
        [uploadimgDelegate performSelector:@selector(postDataProgress:)withObject:self];
    }
}


-(NSString*) getImgViewShortName {
    return self.imageUrlShortName;
}


-(BOOL) isDefaultImage {
    return isDefaultImg;
}


-(BOOL) isChangedImage {
    return isChanged;
}

@end
